﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToDoList.Entity.Models;
using ToDoList.Logic;

namespace ToDoList
{
    public class UserExamine
    {
        /// <summary>
        /// 用户主界面
        /// </summary>
        public void FirstInterface()
        {
            while (true)
            {
                Console.WriteLine(" \t\t1 LoginInterFace //登录\t\t \n" + " \t\t2 RegisterInterface //注册 \t\t\n" +
                                  " \t\t3 Exit \n\t");
                int choose = InputRequire.InputInt("Please input in number your choose(eg:1):");
                Console.Clear();
                switch (choose)
                {
                    case 1: LoginInterFace(); break;
                    case 2: RegisterInterface(); break;
                    case 3: return;
                    default: break;
                }
            }
        }
        /// <summary>
        /// //登录界面 
        /// </summary>
        public void LoginInterFace()
        {
            while (true)
            {
                int ID = InputRequire.InputInt("Enter the user ID:");
                string password = InputRequire.InputString("Enter the password :");

                UserFunction userFunction = new UserFunction();
                try
                {
                    Users user = userFunction.Login(ID, password);
                    if (user != null)
                    {
                        //进入用户功能界面
                        UserExamine userExamine = new UserExamine();
                        userExamine.UserInventoryFunction(user);
                    }
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                int choose = InputRequire.InputInt("Do you want to continue:(1.yes,2.No):");
                if (choose == 2)
                    break;
            }
        }
        /// <summary>
        /// //注册界面
        /// </summary>
        /// 
        public void RegisterInterface()
        {

            #region 输入注册所需要的信息

            string name = InputRequire.InputString("Enter the name:");
            string password = InputRequire.InputString("Enter the password :");
            string nickName = InputRequire.InputString("Enter the nickName :");
            int userID = InputRequire.InputInt("Enter the user ID:");

            #endregion

            #region 判断是否注册成功
            UserFunction userFunction = new UserFunction();
            bool result = userFunction.AddIsDeafault( name, password, nickName);
            if (result)
                Console.WriteLine("You can enroll success!");
            else
                Console.WriteLine("You can not enroll!");
            Console.Clear();
            #endregion
        }
        /// <summary>
        /// //用户清单界面 
        /// </summary>
        /// <param name="user"></param>
        public void UserInventoryFunction(Users user)
        {
            while (true)
            {
                Console.WriteLine("\t\t\t1 Check all the inventory\\ 查看所有的清单 \n\t" +
                                 " \t\t2 Modity a inventory name\\修改清单名称 \n\t" +
                                 "\t\t3 Create a inventory \\创建一个清单\n\t" + //统计未完成的事项
                                 "\t\t4 Find a inventory\\寻找清单 \n\t" +//提醒已过期的事项
                                   "\t\t5 Enter  the backlog things interface\\进入事件界面\n\t" + "\t\t6 Exit \n\t"
                                 );
                int choose = InputRequire.InputInt("Please input in number your choose(eg:1):");
                InventoryFunction userInventory = new InventoryFunction();
                switch (choose)
                {
                    case 1:
                        {
                            var inventorys = userInventory.GetAllInventory(user.Id );
                            foreach (var item in inventorys)
                                Console.WriteLine($"清单名：{item.Name}\t清单ID：{item.Id  }" +
                                        $"清单创建时间{item.CreationTime }\t 用户ID:{item.UserId}");
                        }
                        break;
                    case 2:
                        {
                            string name = InputRequire.InputString("Please input the inventory new name:");
                            int inventoryId = InputRequire.InputInt("Please input the invnetory id");
                            ConsloveFunction(userInventory.ModifyInventoryName(user.Id ,inventoryId, name));
                        }
                        break;
                    case 3:
                        {
                            string name = InputRequire.InputString("Please input the new inventorys name:");
                            DateTime time = Convert.ToDateTime(InputRequire.InputString("Please input the new inventorys time:"));
                            int inventoryId = InputRequire.InputInt("Please input the new inventory id");
                            ConsloveFunction(userInventory.AddInventory(user.Id, name, time));
                        }
                        break;
                    case 4:
                        {
                            int inventoryId = InputRequire.InputInt("Please input the inventory id");
                            var inventory = userInventory.FindInventory(user.Id  ,inventoryId);
                            Console.WriteLine($"清单名：{inventory.Name }\t清单ID：{inventory.Id  }" +
                                      $"清单创建时间{inventory.CreationTime}\t 用户ID:{inventory.UserId}");
                        }
                        break;
                    case 5: EnrollInventoryInterface(user); break;
                    case 6: return;
                    default: break;
                }
            }

        }
        /// <summary>
        /// 用户事件界面
        /// </summary>
        /// <param name="user"></param>
        public void EnrollInventoryInterface(Users user)
        {
            while (true)
            {
                Console.WriteLine(" \t\t\t 1  Add a backlog thing \\添加事项\n\t" +
                                    "\t\t 2 Delete a backlog thing \\删除事项\n\t" +
                                    "\t\t3 Checkall the backlog things\\统计所有的事件\n\t" +
                                   " \t\t4 Statistics all the unfinsihed things\\统计未完成事项   \n\t " +
                                    "\t\t5 Complete the backlog things\\完成某一待办事项 \n\t" +
                                    "\t\t6 Cancel the complete things\\取消完成已完成事项\n\t" +
                                    "\t\t7 Add over time\\添加过期时间  \n\t" +
                                    "\t\t8Add remark \\添加备注Change overtime \n\t" +
                                    "\t\t9 Find the backolg thing \\查找事件\n\t" +
                                    " \t\t10Exit 退出 \n\t ");
                int choose = InputRequire.InputInt("Please input in number your choose(eg:1):");
                EventFunction userEvents = new EventFunction();
                switch (choose)
                {
                    case 1:
                        {
                            int userID = InputRequire.InputInt("Input the user ID");
                            int invnetoryID = InputRequire.InputInt("Input the inventory ID");
                            int backlogID = InputRequire.InputInt("Input the backlog ID:");
                            string content = InputRequire.InputString("Input the content thing");
                            string remark = InputRequire.InputString("Input the remark");
                            DateTime time = InputRequire.InputDateTime("Input the Overtime");
                            ConsloveFunction(userEvents.AddEvent(userID, invnetoryID,  content, time, remark));
                        }
                        break;
                    case 2:
                        userEvents.DeleteEvent(InputRequire.InputInt("Input the backlog ID:"));
                        break;
                    case 3:
                        ConsloveWritelineListBacklog(userEvents.GetAllEvent(user.Id ));
                        break;
                    case 4:
                        ConsloveWritelineListBacklog(userEvents.GetUnfinishedEvent(user.Id ));
                        break;
                    case 5:
                        ConsloveFunction(userEvents.FinishedEvent(InputRequire.InputInt("Input the backlog ID:")));
                        break;
                    case 6:
                        ConsloveFunction(userEvents.CancelFinished(InputRequire.InputInt("Input the backlog ID:")));
                        break;
                    case 7:
                        ConsloveFunction(userEvents.AddOverTime(InputRequire.InputInt("Input the backlog ID:"), InputRequire.InputDateTime("Input the datetime :")));
                        break;
                    case 8:
                        ConsloveFunction(userEvents.AddRemark(InputRequire.InputInt("Input the backlog ID:"), InputRequire.InputString("Input the remark:")));
                        break;
                    case 9:
                       ConsloveWritelineBacklog(userEvents.FindBacklog(user.Id));
                        break;
                    case 10: return;
                    default: break;
                }
            }
        }

        public void ConsloveFunction(bool result)
        {
            if (result)
                Console.WriteLine("Succeed!");
            else
                Console.WriteLine("Default");
            Console.ReadKey();
        }
        public void ConsloveWritelineBacklog(Matters  backlog)
        {
            Console.WriteLine($"事件用户ID：{backlog.UserId }  事件清单ID：{backlog.InventoryId  }  事件ID：{backlog.Id }" +
                $"  事件创建时间{backlog.CreationTime}  事件结束时间：{backlog.OverTime} 事件：{backlog.MatterName  }" +
                $"  事件备注： {backlog.Reamrk }\n");
        }
        public void ConsloveWritelineListBacklog(List<Matters > listBacklog)
        {
            foreach (var item in listBacklog)
                ConsloveWritelineBacklog(item);
        }
    }
}
